# -*- coding = utf-8 -*-
# @Time : 2023/5/9 15:48
# @Author :Henry L
# @File : HLJ.py
# @Software : PyCharm
import glob
import os
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait


class HLJ:
    def __init__(self):
        options = webdriver.ChromeOptions()
        prefs = {"plugins.always_open_pdf_externally": True,
                 "browser.download.dir": 'E:\Algorithm\Spider\ZBXX\HLJ'}
        options.add_experimental_option("prefs", prefs)
        self.url = 'https://ggzyjyw.hlj.gov.cn/jyfwdt/003002/003002005/003002005002/about2.html'
        self.driver = webdriver.Chrome(options=options)
        self.driver.maximize_window()
        self.wait = WebDriverWait(driver=self.driver, timeout=10)
        self.driver.get(self.url)

    def get(self, page):
        time.sleep(3)
        lan = self.driver.find_elements(By.XPATH, "/html/body/div[2]/div/div/div[3]/div/div[2]/div/div/ul/li/div")
        for item in lan:
            time.sleep(3)
            if item.find_element(By.TAG_NAME, "a").text == '关于黑龙江省公共资源交易平台各类咨询电话变更的公告':
                continue
            item.find_element(By.TAG_NAME, "a").click()
            handles = self.driver.window_handles
            self.driver.switch_to.window(handles[-1])
            time.sleep(1.5)
            title = self.driver.find_element(By.XPATH, "/html/body/div[2]/div/div/div[2]/div/h2").text
            box = self.driver.find_element(By.XPATH, "/html/body/div[2]/div/div/div[2]/div")
            date = box.find_element(By.XPATH, "div[1]/div/div[3]").text.replace("日期：", '')
            view_time = self.driver.find_element(By.ID, "infoViewCount").text
            content = box.find_element(By.CLASS_NAME, "ewb-art-bd").get_attribute("outerHTML")
            attachments = box.find_elements(By.TAG_NAME, "a")
            attachments_list = []
            if len(attachments) > 0:
                for a in attachments:
                    if a.text == "" or a.get_attribute("href") is None:
                        continue
                    elif "附件" in a.text:
                        a.click()
                        a_name = a.get_attribute("href").replace("/hljggzy/", '').replace(a.text, '')
                        attachments_list.append(a_name)
                        self.get_last_filename_and_rename('./', a_name)
            self.driver.close()
            handles = self.driver.window_handles
            self.driver.switch_to.window(handles[-1])
            data_list = [title, date, view_time, content, attachments_list]
            print(data_list)

    def get_last_filename_and_rename(self, sav_folder, new_filename):
        files = glob.glob(sav_folder + '/*')
        max_file = max(files, key=os.path.getctime)
        filename = max_file.split("/")[-1].split(".")[0]
        new_path = max_file.replace(filename, new_filename)
        os.rename(max_file, new_path)
        return new_path

    def close(self):
        time.sleep(60)
        self.driver.quit()

hlj = HLJ()
hlj.get("")
hlj.close()
